<?php
class arcController extends accessController{
    public static function _right(){
        return array(
            array('文章管理',1),
            array('文章添加','title_unique,add,add_'),
            array('文章列表','arc_preview,mylist'),
            array('文章编辑','mod,mod_'),
            array('文章删除','del'),
            array('静态生成','html,html_,make_html'),
            array('批量操作','bit_op'),
            array('导入导出','dump,dump_'),
        );
    }
	public function __construct(){
		parent::__construct();
		$this->mid=11;
	}
	public function s(){
		return array('arc','attrtaglist');
	}
	//标题检测
	public function title_unique(){
		$title=$_POST['title'];
		$id=$_POST['id'];
		$echo=array();
		if($title){
			$r=$this->s_0->ajax_title_check($title,$id);
			if($r>0){
				$echo[0]=0;//文章已经存在
			}else{
				$echo[0]=1;//文章可以使用
			}
		}
		echo json_encode($echo);
		exit;
	}
	public function arc_preview(){
		$url=trim($_GET['url']);
		if(file_exists(ROOT.trim($url,'/'))){
			js("window.location.href='{$url}';");
			exit;
		}
		alert('文件不存在！');
		back();
	}
	public function add(){
        $selected_c=intval($_GET['selected_c']);
        $category_service=load_service('category');
		$category_service->define_table($this->mid);
        $category_arr=$category_service->with_option($this->mid);
		
		$mysort=time();
        $flag_data=$this->s_0->flag_data();
		include($this->viewer_dir.'arc/add.php');
	}
	public function add_(){
    	if($_POST){
			$r=$this->s_0->add_($_POST);
			autoalert($r[0],$r[1]);
            //插入标签
			if($r[0]){
				$this->s_1->add_($r[2],$_POST['tagattr']);
			}
			back();
        }
    }
	public function mylist(){
	    
		$flag_data=$this->s_0->flag_data();
		$flag_data['-1']='--请选择--';
		$status_data=array('-1'=>'--请选择--','0'=>'隐藏','1'=>'显示');
		
		$condition=array();
		$extra='';
		$extra_data=array();
		
		$arccat_id=intval($_GET['arccat_id']);//
		if($arccat_id){
			$condition['arccat_id']=$arccat_id;
			$extra.=" and `arccat_id`=:arccat_id";
			$extra_data['arccat_id']=$arccat_id;
		}
		
		isset($_GET['flag']) or $_GET['flag']=-1;
		$flag=intval($_GET['flag']);//
		if($flag>-1){
			$condition['flag']=$flag;
			$extra.=" and `flag`=:flag";
			$extra_data['flag']=$flag;
		}
		
		isset($_GET['status']) or $_GET['status']=-1;
		$status=intval($_GET['status']);//
		if($status>-1){
			$condition['status']=$status;
			$extra.=" and `status`=:status";
			$extra_data['status']=$status;
		}
		
		$add_time_1=$_GET['add_time_1'];//
		if($add_time_1){
			$condition['add_time_1']=$add_time_1;
			$tmp=strtotime($add_time_1);
			$extra.=" and `add_time`>=:at_1";
			$extra_data['at_1']=$tmp;
		}
		$add_time_2=$_GET['add_time_2'];//
		if($add_time_2){
			$condition['add_time_2']=$add_time_2;
			$tmp=strtotime($add_time_2);
			$extra.=" and `add_time`<=:at_2";
			$extra_data['at_2']=$tmp;
		}
		
		$edit_time_1=$_GET['edit_time_1'];//
		if($edit_time_1){
			$condition['edit_time_1']=$edit_time_1;
			$tmp=strtotime($edit_time_1);
			$extra.=" and `edit_time`>=:et_1";
			$extra_data['et_1']=$tmp;
		}
		$edit_time_2=$_GET['edit_time_2'];//
		if($edit_time_2){
			$condition['edit_time_2']=$edit_time_2;
			$tmp=strtotime($edit_time_2);
			$extra.=" and `edit_time`<=:et_2";
			$extra_data['et_2']=$tmp;
		}
		
		$id=intval($_GET['id']);//
		if($id){
			$condition['id']=$id;
			$extra.=" and `id`=:id";
			$extra_data['id']=$id;
		}
		
		$title=trim($_GET['title']);//
		if($title){
			$condition['title']=$title;
			$extra.=" and `title` like :title";
			$extra_data['title']="%{$title}%";
		}
		
        $category_service=load_service('category');
		$category_service->define_table($this->mid);
		
        $category_arr=$category_service->with_option($this->mid);
		
		$return=$this->s_0->li($condition,$extra,$extra_data);
		include($this->viewer_dir.'arc/mylist.php');
	}
	public function mod(){
        $id=intval($_GET['id']);
        if($id>0){
		
			$category_service=load_service('category');
			$category_service->define_table($this->mid);
			$category_arr=$category_service->with_option($this->mid);
		
            $data=$this->s_0->mod($id);
            if($data){
                $flag_data=$this->s_0->flag_data();//flag属性
                $data['tagattr']=$this->s_1->mod($id);//文章属性 美文,小说
                include($this->viewer_dir.'arc/mod.php');
            }
        }
    }
    public function mod_(){
		$id=intval($_GET['id']);
        if($_POST and $id>0){
            $r=$this->s_0->mod_($_POST,$id);
            autoalert($r[0],$r[1]);
            //编辑标签
			if($r[0]){
				$this->s_1->mod_($id,$_POST['tagattr']);
			}
            back();
        }
    }
	public function del(){
		$id=intval($_GET['id']);
        if($id>0){
            $r=$this->s_0->del($id);
            autoalert($r[0],$r[1]);
            back();
        }
	}
	//生成html 列表页调用
	public function make_html(){
		$id=intval($_GET['id']);
		if($id==0){
			alert('id error!');
			back();
		}
		$this->s_0->make_html(array($id));
		alert('生成成功！');
		back();
	}
	public function html(){
		
		list($fid,$tid)=$this->s_0->return_min_max();
		$fid=intval($fid);
		$tid=intval($tid);
		$num=100;
		$category_service=load_service('category');
		$category_service->define_table($this->mid);
		$category_arr=$category_service->with_option($this->mid);
		
		include($this->viewer_dir.'arc/html.php');
	}
	public function html_(){
		$k=trim($_GET['k']);
		switch($k){
			case 'all':
			$this->s_0->all();
			break;
			case 'index':
			$this->s_0->html_index();
			break;
			case 'list':
			$this->s_0->html_list();
			break;
			case 'show':
			$this->s_0->html_show();
			break;
			case 'update':
			$this->s_0->html_update();
			break;
			case 'cate':
			$this->s_0->html_cate();
			break;
			case 'item':
			$this->s_0->html_item();
			break;
			default:
			exit('k error!');
			break;
		}
	}
	//批量操作 文章列表页调用
	public function bit_op(){
		if(empty($_POST)) exit;
		$k=$_GET['k'];
		$items=$_POST['items'];
		if(empty($items)) exit;
		switch($k){
			case 'update_address'://已做
			$this->s_0->update_address($items);
			break;
			case 'make_html'://已做
			$this->s_0->make_html($items);
			break;
			case 'update_flag'://已做
			$this->s_0->update_flag($items,$_POST['flag']);
			break;
			case 'update_status'://已做
			$this->s_0->update_status($items,$_POST['status']);
			break;
			case 'export':
			$this->s_0->dump_export($items,1,'single');
			break;
			default:
			exit('k error!');
			break;
		}
		alert('操作成功！');
		back();
	}
	public function dump(){
		$min_max=$this->s_0->return_min_max();
		$fields=$this->s_0->get_fields();
		include($this->viewer_dir.'arc/dump.php');
	}
	public function dump_(){
		if(empty($_POST)) exit;
		$type=$_GET['type'];
		
		switch($type){
			case 1:
			//导出
			$start=$_POST['start'];
			$stop=$_POST['stop'];
			$with_title=$_POST['with_title'];
			$r=$this->s_0->dump_export(array('start'=>$start,'stop'=>$stop),$with_title,'range');
			autoalert($r[0],$r[1]);
            back();
			break;
			
			case 2:
			//导入
			$myconfig=$_POST['myconfig'];
			$r=$this->s_0->dump_import($myconfig);
			autoalert($r[0],$r[1]);
            back();
			break;
		}
	}
//
}